import { Component, OnInit } from '@angular/core';
import { DailyaggregationService, DailyAggregation } from 'src/app/service/dailyaggregation.service';
import { DatePipe } from '@angular/common';
import { NzNotificationService } from 'ng-zorro-antd';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-dailyaggregation',
  templateUrl: './dailyaggregation.component.html',
  styleUrls: ['./dailyaggregation.component.css'],
  providers: [ DatePipe ]
})
export class DailyaggregationComponent implements OnInit {

  dateRange:Date[];
  mill:string = '朗迪印染';
  tableData:DailyAggregation[];
  sdate:string;
  edate:string;

  constructor(
    private service:DailyaggregationService,
    private datePipe:DatePipe,
    private noti:NzNotificationService,
    private route:ActivatedRoute
  ) { 
    this.sdate = this.route.snapshot.queryParams['sdate'];
    this.edate = this.route.snapshot.queryParams['edate'];

    this.dateRange = [];
  }

  ngOnInit() {
  }

  onChangeDateRange(event){
    console.log(this.dateRange);
  }

  getAggregation(){
    if( this.dateRange.length <=0 ){
      this.noti.create('warning','警告','没有设置日期区间！');
      return
    }

    if( this.mill.length <=0 ){
      this.noti.create('warning','警告','没有设置染厂！');
      return
    }

    const sdate = this.datePipe.transform(this.dateRange[0],'yyyy-MM-dd HH:mm:ss');
    const edate = this.datePipe.transform(this.dateRange[1],'yyyy-MM-dd HH:mm:ss');

    this.service.getAggregation(sdate,edate,this.mill).subscribe(
      ( res:DailyAggregation[] ) =>{
        this.tableData = res;
        console.log('异步获取到的数据是：');
        console.log(res);
      },error => console.log('获取日报表数据时出错！')
    )
  }


  /**
   * 打开白坯详情
   * @param event 
   */
  openBP(rowIndex:number){
    if( rowIndex ===0 ) {
      this.noti.create('info','通知','点击库存不弹出！');
      return
    }

    const index = rowIndex;
    const date:string = this.tableData[index]["billdate"].toString()
    this.noti.create('info','白坯','日期 = ' + date);
  }

  /**
   * 打开成品详情
   * @param event 
   */
  openCP(rowIndex:number){
    this.noti.create('info','成品','行号 = ' + rowIndex);
  }
}
